<?php
	/**
	 +=========================================================
	 * 零度空间 ZD cms 网站管理系统
	 * PublicAction.class.php 共用模块
	 +=========================================================
	 * @copyright © 2012 nickdraw.com All rights reserved.
	 * @author NickDraw(零度温柔) webmaster@206c.net
	 * @license http://www.nickdraw.com/license
	 +=========================================================
	 */

	class PublicAction extends Action {
		// 检查用户是否登录
		protected function checkUser() {
			if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
				$this->assign('jumpUrl', U('login'));
				$this->error('没有登录');
			}
		}
		
		// 用户登录页面
		public function login() {
			if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
				$this->display();
			}else{
				redirect(__APP__);
			}
		}
		
		public function index()
		{
			//如果通过认证跳转到首页
			redirect(__APP__);
		}
	
		// 用户登出
	    public function logout()
	    {
	        if(isset($_SESSION[C('USER_AUTH_KEY')])) {
				unset($_SESSION['loginId']);
				unset($_SESSION[C('USER_AUTH_KEY')]);
				unset($_SESSION);
				session_destroy();
	            $this->assign("jumpUrl", U('login') );
	            $this->success('登出成功！');
	        }else {
	            $this->error('已经登出！');
	        }
	    }
	
		// 登录检测
		/*public function checkLogin() {
			if(empty($_POST['account'])) {
				$this->error('帐号错误！');
			}elseif (empty($_POST['password'])){
				$this->error('密码必须！');
			}elseif (''===trim($_POST['verify'])){
				$this->error('验证码必须！');
			}
	        //生成认证条件
	        $map            =   array();
			// 支持使用绑定帐号登录
			$map['account']	= $_POST['account'];
	        $map["status"]	=	array('gt',0);
	        if($_SESSION['verify'] != md5($_POST['verify'])) {
	            $this->error('验证码错误！');
	        }
			import('ORG.Util.RBAC');
	        $authInfo = RBAC::authenticate($map);
	        //使用用户名、密码和状态的方式进行认证
	        if(false === $authInfo) {
	            $this->error('帐号不存在或已禁用！');
	        }else {
	            if($authInfo['password'] != md5($_POST['password'])) {
	            	$this->error('密码错误！');
	            }
				$_SESSION[C('USER_AUTH_KEY')]	=	$authInfo['id'];
	            $_SESSION['email']				=	$authInfo['email'];
	            $_SESSION['loginUserName']		=	$authInfo['nickname'];
	            $_SESSION['lastLoginTime']		=	$authInfo['last_login_time'];
				$_SESSION['login_count']		=	$authInfo['login_count'];
				$_SESSION['user_type']    		=  $authInfo['type_id'];
				$_SESSION['loginUserID'] 		= $authInfo['account'];
	            if($authInfo['account']=='admin') {
	            	$_SESSION['administrator']		=	true;
	            }
				
	            //保存登录信息
				$User	=	M('User');
				$ip		=	get_client_ip();
				$time	=	time();
	            $data = array();
				$data['id']	=	$authInfo['id'];
				$data['last_login_time']	=	$time;
				$data['login_count']	=	array('exp','(login_count+1)');
				$data['last_login_ip']	=	$ip;
				$User->save($data);
	            
				// 缓存访问权限
	            RBAC::saveAccessList();
				//缓存菜单
				$this->menuAccess($authInfo['id']);
				$this->leftmenu();
				$this->success('登录成功！');
			}
		}*/
		
		public function checkLogin() {
	        //生成认证条件
	        $map            =   array();
			// 支持使用绑定帐号登录
			$map['account']	= $_POST['account'];
	        $map["status"]	=	array('gt',0);
			import('ORG.Util.RBAC');
	        $authInfo = RBAC::authenticate($map);
	        //使用用户名、密码和状态的方式进行认证
	        if(false === $authInfo) {
	            $this->error('帐号不存在或已禁用！');
	        }else {
	            if($authInfo['password'] != md5($_POST['password'])) {
	            	$this->error('密码错误！');
	            }
				$_SESSION[C('USER_AUTH_KEY')]	=	$authInfo['id'];
	            $_SESSION['email']				=	$authInfo['email'];
	            $_SESSION['loginUserName']		=	$authInfo['nickname'];
	            $_SESSION['lastLoginTime']		=	$authInfo['last_login_time'];
				$_SESSION['login_count']		=	$authInfo['login_count'];
				$_SESSION['user_type']    		=  $authInfo['type_id'];
				$_SESSION['loginUserID'] 		= $authInfo['account'];
	            if($authInfo['account']=='admin') {
	            	$_SESSION['administrator']		=	true;
	            }
				
	            //保存登录信息
				$User	=	M('User');
				$ip		=	get_client_ip();
				$time	=	time();
	            $data = array();
				$data['id']	=	$authInfo['id'];
				$data['last_login_time']	=	$time;
				$data['login_count']	=	array('exp','(login_count+1)');
				$data['last_login_ip']	=	$ip;
				$User->save($data);
	            
				// 缓存访问权限
	            RBAC::saveAccessList();
				//缓存菜单
				$this->menuAccess($authInfo['id']);
				$this->leftmenu();
				$this->success('登录成功！');
			}
		}

		private function leftmenu(){
			
			$this->checkUser();
			if(isset($_SESSION[C('USER_AUTH_KEY')])) {
				$node    =   M("Node");
				$id	=	$node->getField("id");
				$where['level']=2;
				$where['status']=1;
				$where['pid']=$id;
	            $list	=	$node->where($where)->field('id,name,group_id,title')->order('sort asc')->select();
	            $accessList = $_SESSION['_ACCESS_LIST'];
	            foreach($list as $key=>$module) {
	                 if(isset($accessList[strtoupper(APP_NAME)][strtoupper($module['name'])]) || $_SESSION['administrator']) {
	                    //设置模块访问权限
	                    $module['access'] =   1;
	                    $menu[$key]  = $module;
	                }
	            }
	            //缓存菜单访问
	            $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]]	=	$menu;
			}
		}

		
		//加载权限菜单
		private function menuAccess($authID){
			
			$this->checkUser();
			
			$role_model = M('role_user');
			$menu_model = M('access_menu');
			$p_model = M('group');
			$p_menu = array();
			
			if ($_SESSION['administrator'] == true){
				$p_list = $p_model->where("status=1")->field('id,name,title,pid')->order('sort ASC')->select();
				foreach ($p_list as $adminkey => $adminval) {
					$p_menu[$adminval['id']] = $adminval;
				}
			}else{
				$role_list = $role_model->where("user_id='".$authID."'")->select();
				if (!empty($role_list)){
					
					$p_array = array();
					foreach ($role_list as $key => $val) {
						$menu_list = $menu_model->where("role_id='".$val['role_id']."'")->select();
						foreach ($menu_list as $a => $v) {
							$p_array[] = $v['group_id'];
						}
					}
					//获取菜单列表
					foreach ($p_array as $kp => $vp) {
						$p_list = $p_model->where("id='".$vp."' AND status=1")->field('id,name,title,pid')->order('sort ASC')->select();
						foreach ($p_list as $ap => $lp) {
							$p_menu[] = $lp;
						}
					}
				}
			}
			
			if (isset($p_menu)){
				$_SESSION['au_menu_list'] = $p_menu;
			}else{
				$_SESSION['au_menu_list'] = '';
			}
		}

	
	    // 更换密码
	    public function changePwd()
	    {
			$this->checkUser();
	        //对表单提交处理进行处理或者增加非表单数据
			if(md5($_POST['verify'])	!= $_SESSION['verify']) {
				$this->error('验证码错误！');
			}
			$map	=	array();
	        $map['password']= pwdHash($_POST['oldpassword']);
	        if(isset($_POST['account'])) {
	            $map['account']	 =	 $_POST['account'];
	        }elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {
	            $map['id']		=	$_SESSION[C('USER_AUTH_KEY')];
	        }
	        //检查用户
	        $User    =   M("User");
	        if(!$User->where($map)->field('id')->find()) {
	            $this->error('旧密码不符或者用户名错误！');
	        }else {
				$User->password	=	pwdHash($_POST['password']);
				$User->save();
				$this->assign('jumpUrl', U('main') );
				$this->success('密码修改成功！');
	         }
	    }
	
		// 验证码显示
	    public function verify()
	    {
	        import("ORG.Util.Image");
	        Image::buildImageVerify(4);
	    }
	
	}
?>